<html>
    <head>
        <title>Nguyen Truong Huy</title>
    </head>
    <body>
        <?php require_once './model/dp2.php'; ?>
        <h1>Nguyen Truong Huy</h1>
        <?php
        $query = "SELECT * FROM companies";
        $companies = getpages($query);
        $query = "SELECT * FROM apps";
        $apps = getpages($query);
        ?>
        <form method="POST"><table>
                Keyword  <input type="text" name="key"> <br>
                Min Like <input type="text"name="min"> <br>
                Max Like <input type="text"name="max"> <br>
                Company <select name="company">

                    <option value="0">select company</option>
                    <?php foreach ($companies as $company): ?>
                        <option value="<?php echo $company['id'] ?>"><?php echo $company['name'] ?></option>
                    <?php endforeach; ?>
                </select>

                Application :<select name="id_apps">
                    <option value="0">select Application</option>
                    <?php foreach ($apps as $app): ?>
                        <option value="<?php echo $app['id'] ?>"><?php echo $app['name'] ?></option>
                    <?php endforeach; ?>
                </select>

                <?php
                $query = "SELECT * FROM apps";
                $apps = getpages($query);
                ?>

                <?php foreach ($apps as $app): ?>
                    <option value="<?php echo $app['id']; ?>"><br>
                    <?php echo $app['name']; ?>
                <?php endforeach; ?>
                </option>
                </select>

                <input type="submit"name="submit"value="Seach"> <br> 
                </form>
                <?php
                require_once './model/dp2.php';

                $query = "SELECT * FROM pages LIMIT 10";
                $pages = getpages($query);
                ?>
                <?php if (isset($_POST['submit'])): ?>
                    <?php
                    $min = (double) $_POST['min'];
                    $max = (double) $_POST['max'];
                    $keyword = $_POST['key'];
                    $company = $_POST["company"];
                    $apps = $_POST["id_apps"];

                    if (!empty($company)) //empty ktra rong kg.
                        $company = "(companies.id=$company) AND";
                    else
                        $company = "";
                    if (!empty($apps))
                        $apps = "(companies.id=$apps) AND";
                    else
                        $apps = "";

                    if (empty($max) && !empty($min)) {
                        $query = "SELECT * FROM pages
            INNER JOIN apps_pages ON pages.id=apps_pages.id_page
            INNER JOIN apps ON apps.id=apps_pages.id_app
            INNER JOIN companies ON companies.id=apps.id_company
            where $company
                  $apps 
                  (pages.likes>= $min_fans )and
                  (pages.description LIKE '% $keyword %')
                  LIMIT 0,10";
                    } else if (!empty($max) && empty($min)) {
                        $query = "SELECT * FROM pages
            INNER JOIN apps_pages ON pages.id=apps_pages.id_page
            INNER JOIN apps ON apps.id=apps_pages.id_app
            INNER JOIN companies ON companies.id=apps.id_company
            where $company
                  $apps
                 ( pages.likes<= $max )and
                  (pages.description LIKE '% $keyword %')
                  LIMIT 0,10";
                    } else if (empty($max) && empty($min)) {
                        $query = "SELECT * FROM pages
            INNER JOIN apps_pages ON pages.id=apps_pages.id_page
            INNER JOIN apps ON apps.id=apps_pages.id_app
            INNER JOIN companies ON companies.id=apps.id_company
            where $company
                  $apps
                 ( pages.description LIKE '% $keyword %')
                  LIMIT 0,10";
                    } else {
                        $query = "SELECT * FROM pages
            INNER JOIN apps_pages ON pages.id=apps_pages.id_page
            INNER JOIN apps ON apps.id=apps_pages.id_app
            INNER JOIN companies ON companies.id = apps.id
            where $company
                  $apps
                  (pages.likes>= $min) AND
                  (pages.likes<= $max) AND
                  pages.description LIKE '% $keyword %'
                  LIMIT 0,10";
                    }

                    $pages = getpages($query);
                    var_dump($query);
                    ?>
                <?php else : ?>
                    <?php
                    $query = "SELECT * FROM pages LIMIT 0, 10";
                    $pages = getpages($query);
                    ?>
                    <?php ?>
                <?php endif; ?>
                <?php if (!empty($pages)): ?>
                    <table border ="1">
                        <tr>
                            <th>PID</th>
                            <th>PICTURE</th>
                            <th>DESCRIPTION</th>
                            <th>NAME</th>
                            <th>USERNAME</th>
                            <th>PHONE</th>
                            <th>CITY</th>
                            <th>LIKE</th>

                        </tr>
                        <?php foreach ($pages as $page): ?>
                            <tr>
                                <td><?php echo $page['pid'] ?></td>
                                <td><img src='<?php echo $page['picture'] ?>'></td>
                                <td><?php echo substr($page ['description'], 0, 100) ?></td>
                                <td><?php echo $page['name'] ?></td>
                                <td><?php echo $page['username'] ?></td>
                                <td><?php echo $page['phone'] ?></td>
                                <td><?php echo $page['city'] ?></td> 
                                <td><?php echo $page['likes'] ?></td> 
                            </tr>
                        <?php endforeach; ?>
                    </table>
                <?php endif; ?>
        <script type ="text/javascript">
            alert("huybee")
        </script>
                </body>
                </html>


